﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace RomanConvertKata
{
    class AtomicRomanNumberChar
    {
        internal static readonly AtomicRomanNumberChar[] AtomicRomanNumbers =
        {
            new AtomicRomanNumberChar('I', 1), new AtomicRomanNumberChar('V', 5), new AtomicRomanNumberChar('X', 10),
            new AtomicRomanNumberChar('L', 50), new AtomicRomanNumberChar('C', 100),
        };
        internal static readonly AtomicRomanNumberChar[] LegalMinusAtomicRomanNumbers =
            {new AtomicRomanNumberChar('X', 10), new AtomicRomanNumberChar('I', 1),};

        public static int ConvertRomanCharToInt(char romanChar)
        {
            switch (romanChar)
            {
                case 'I': return 1;
                case 'V': return 5;
                case 'X': return 10;
                case 'L': return 50;
                case 'C': return 100;
            }
            throw new ArgumentException($"Unexpected Input {romanChar}");
        }

        public static AtomicRomanNumberChar ConvertAtomicDecimalNumberToRomanNumber(int atomicDecimalNumber)
        {
            return AtomicRomanNumbers.First(_ => _.DecimalNumber == atomicDecimalNumber);
        }

        public AtomicRomanNumberChar GetNextAtomicRomanNumber()
        {
            var currentAtomicRomanIndex = AtomicRomanNumbers
                .Select((romanNumber, index) => new { romanNumber, index })
                .First(_ => _.romanNumber.DecimalNumber == DecimalNumber).index;
            var nextAtomicRomanNumber = AtomicRomanNumbers[currentAtomicRomanIndex + 1];
            return nextAtomicRomanNumber;
        }


        public AtomicRomanNumberChar(char romanNumberChar, int decimalNumber)
        {
            DecimalNumber = decimalNumber;
            RomanNumberChar = romanNumberChar;
        }

        public IEnumerable<AtomicRomanNumberChar> GetLegalMinusAtomicRomanNumbersLessThanAtomicDecimalNumber()
        {
            return LegalMinusAtomicRomanNumbers.Where(_ => _.DecimalNumber < DecimalNumber);
        }

        public char RomanNumberChar { get; }
        public int DecimalNumber { get; }

    }
}
